﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace EventReminder.Server
{
    public class UsersManager
    {
        List<Users> _userlist;

        public UsersManager()
        {
            _userlist = new List<Users>();
            GetUserData();

        }

        public int GetUserCount
        {
            get { return _userlist.Count; }
        }

        public Users GetUser(int id)
        {
            Users returnuser = new Users();

            foreach (Users u in _userlist)
            {
                if (u.GetUserID == id)
                {
                    returnuser = u;
                    break;
                }
            }

            return returnuser;
        }

        public List<string> GetUserList()
        {
            List<string> returndata = new List<string>();
            string data;

            foreach (Users u in _userlist)
            {
                data = u.FirstName + " " + u.LastName + " " + u.UserEmail;
                returndata.Add(data);
            }

            return returndata;
        }

        public bool AddNewUser(Users u)
        {
            bool success = false;

            try
            {
                _userlist.Add(u);
                success = true;
            }
            catch
            {

            }
            SaveUserData();

            return success;
        }

        public bool ValidateUser(string uname, string pswd)
        {
            bool valid = false;

            foreach(Users u in _userlist)
            {
                if (uname == u.UserName && pswd == u.UserPassword)
                {
                    valid = true;
                    break;
                }
            }

            return valid;
        }

        public void SetSessionID(string uname, string session)
        {
            foreach (Users u in _userlist)
            {
                if (u.UserName == uname)
                {
                    u.GetSessionID = session;
                    break;
                }
            }
        }

        private void GetUserData()
        {
            try
            {
                _userlist.Clear();

                using (StreamReader textIn = new StreamReader(
                    new FileStream(
                        @"..\..\..\data\userinfo.txt", FileMode.Open, FileAccess.Read)))
                {
                    while (textIn.Peek() != -1)
                    {
                        string row = textIn.ReadLine();
                        Users u = CreateUser(row);
                        _userlist.Add(u);
                    }
                }
            }

            catch
            {
                //MessageBox.Show("There was a problem with the file.", "Error");
            }
        }

        public void LogUserOut(string sid)
        {

            foreach (Users u in _userlist)
            {
                if (u.GetSessionID == sid)
                {
                    u.GetSessionID = "Logout";
                    break;
                }
                
            }

            SaveUserData();
            GetUserData();
        }

        public void SaveUserData()
        {
            try
            {
                using (StreamWriter sw = new StreamWriter(
                    new FileStream(
                        @"..\..\..\Data\userinfo.txt", FileMode.Create, FileAccess.Write)))
                    foreach (Users u in _userlist)
                    {
                        sw.WriteLine(WriteUser(u));
                    }
            }

            catch
            {
                //MessageBox.Show("There was a problem with the file.", "Error");
            }

            GetUserData();
        }

        private Users CreateUser(string data)
        {
            string[] dataline;
            dataline = data.Split('|');
            Users u = new Users();

            u.GetUserID = Convert.ToInt32(dataline[0]);
            u.FirstName = dataline[1];
            u.LastName = dataline[2];
            u.UserEmail = dataline[3];
            u.UserName = dataline[4];
            u.UserPassword = dataline[5];
            u.GetSessionID = dataline[6];
            return u;
        }

        private string WriteUser(Users u)
        {
            string returnvalue = "";

            returnvalue = u.GetUserID.ToString() + "|";
            returnvalue += u.FirstName + "|";
            returnvalue += u.LastName + "|";
            returnvalue += u.UserEmail + "|";
            returnvalue += u.UserName + "|";
            returnvalue += u.UserPassword + "|";
            returnvalue += u.GetSessionID;

            return returnvalue;

        }
    }
}
